//
//  QualityControlViewController.m
//  MyProject
//
//  Created by Pei on 16/6/19.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "QualityControlViewController.h"
#import "UIView+Additions.h"
#import "IHLabel.h"
#import "UILabel+Utilities.h"
#import "CheckAndAcceptViewController.h"
#import "QualityScanViewController.h"
#import "UserInfo.h"
#import "UploadGongXuViewController.h"
#import "SureGongXuViewController.h"
#import "ShiTiQKCJViewController.h"
#import "UploadShiTiQKCJViewController.h"
#import "UIImageView+WebCache.h"

@interface QualityControlViewController ()
{
    UIImageView *_introImageView;
    
    NSString *_currentAction;
}

@end

@implementation QualityControlViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Not_Hide_TabBar_Notification object:nil];
}


- (void)viewDidLoad {
    
    self.navTitle = @"质量监控";
    
    [super viewDidLoad];
    
    /*
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"创建" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
     */
    
    
    
    [self initTopView];
    [self initContentView];
}





#pragma mark ************************ UI ***************************
- (void)initTopView
{
    _introImageView = [[UIImageView alloc] init];
    //_introImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"07banner"]];
    _introImageView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 330.0f/2.0f);
    [_introImageView sd_setImageWithURL:[NSURL URLWithString:kAppGetHeaderImage(@"SQCP_App_quality_header")] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
    [self.view addSubview:_introImageView];
}

- (void)initContentView
{
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = CGRectMake(0.0f, _introImageView.frame.size.height, kDeviceWidth, kDeviceHeight-kTabHeight-NAV_HEIGHT-_introImageView.frame.size.height);
    contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:contentView];
    
    float cWidth = (kDeviceWidth-15*2-10)/2.0f;
    
    UIControl *topLeft = [[UIControl alloc] init];
    topLeft.frame = CGRectMake(15.0f, 15.0f, cWidth, cWidth/5.0f*4.0f);
    //关键工序验收:ToBeAcceptenceCriticalActivity
    if ([self isHasUserPrivilege:@"ToBeAcceptenceCriticalActivity"]) {
        topLeft.backgroundColor = kRGB(113, 125, 162);
    }else{
        topLeft.backgroundColor = kNoUserPrivilegeColor;
    }
    //topLeft.backgroundColor = kRGB(113, 125, 162);
    topLeft.layer.cornerRadius = 5.0f;
    topLeft.layer.masksToBounds = YES;
    topLeft.tag = 166;
    [topLeft addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:topLeft];
    
    UIImageView *topLeftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01guanjiangongxu"]];
    topLeftImageView.frame = CGRectMake(20.0f, (topLeft.frame.size.height-80/2-5.0f-40)/2.0f, 132/2.0f, 80.0f/2.0f);
    [topLeft addSubview:topLeftImageView];
    
    IHLabel *topLeftTitleLbl = [[IHLabel alloc] init];
    topLeftTitleLbl.frame = CGRectMake(topLeftImageView.frame.origin.x, topLeftImageView.frame.origin.y+topLeftImageView.frame.size.height+5.0f, 70, 40);
    topLeftTitleLbl.text = @"关键工序查询";
    topLeftTitleLbl.font = kAppFont(Home_Big_Size);
    topLeftTitleLbl.numberOfLines = 0;
    topLeftTitleLbl.textColor = kWhiteColor;
    [topLeft addSubview:topLeftTitleLbl];
    
    UIControl *centerLeft = [[UIControl alloc] init];
    centerLeft.frame = CGRectMake(15.0f, topLeft.frame.origin.y+topLeft.frame.size.height+10.0f, cWidth, cWidth/6.0f*3.0f);
    //关键工序上传:UploadCriticalActivity
    if ([self isHasUserPrivilege:@"UploadCriticalActivity"]) {
        centerLeft.backgroundColor = kRGB(90, 115, 196);
    }else{
        centerLeft.backgroundColor = kNoUserPrivilegeColor;
    }
    //centerLeft.backgroundColor = kRGB(90, 115, 196);
    centerLeft.layer.cornerRadius = 5.0f;
    centerLeft.layer.masksToBounds = YES;
    centerLeft.tag = 169;
    [centerLeft addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:centerLeft];
    
    UIImageView *centerLeftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"03zhuapai"]];
    centerLeftImageView.frame = CGRectMake(20.0f, (centerLeft.frame.size.height-105/3)/2.0f, 122/3.0f, 105.0f/3.0f);
    [centerLeft addSubview:centerLeftImageView];

    
    UILabel *centerLeftTitleLbl = [[UILabel alloc] init];
    centerLeftTitleLbl.frame = CGRectMake(centerLeftImageView.right+5.0f, centerLeftImageView.origin.y-2.0f, 69.0f, 40);
    centerLeftTitleLbl.text = @"关键工序上传";
    centerLeftTitleLbl.font = kAppFont(Home_Big_Size);
    centerLeftTitleLbl.textColor = kWhiteColor;
    centerLeftTitleLbl.numberOfLines = 0;
    [centerLeft addSubview:centerLeftTitleLbl];
    
    
    
    UIControl *bottomLeft = [[UIControl alloc] init];
    bottomLeft.frame = CGRectMake(15.0f, centerLeft.frame.origin.y+centerLeft.frame.size.height+10.0f, cWidth, cWidth/7.0f*6.0f*2.0f+10.0f-topLeft.height-centerLeft.height-10.0f*2.0f);
    //实体情况上传:UploadProjectConstrution
    if ([self isHasUserPrivilege:@"UploadProjectConstrution"]) {
        bottomLeft.backgroundColor = kRGB(12, 94, 250);
    }else{
        bottomLeft.backgroundColor = kNoUserPrivilegeColor;
    }
    //bottomLeft.backgroundColor = kRGB(12, 94, 250);
    bottomLeft.layer.cornerRadius = 5.0f;
    bottomLeft.layer.masksToBounds = YES;
    bottomLeft.tag = 111;
    [bottomLeft addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:bottomLeft];
    
    UILabel *bottomLeftTitleLbl = [[UILabel alloc] init];
    bottomLeftTitleLbl.frame = CGRectMake(0.0f, (bottomLeft.height-Home_Big_Size)/2.0f, bottomLeft.width, Home_Big_Size);
    bottomLeftTitleLbl.text = @"实体情况上传";
    bottomLeftTitleLbl.font = kAppFont(Home_Big_Size);
    bottomLeftTitleLbl.textColor = kWhiteColor;
    bottomLeftTitleLbl.textAlignment = NSTextAlignmentCenter;
    [bottomLeft addSubview:bottomLeftTitleLbl];
    
    
    
    
    
    UIControl *topRight = [[UIControl alloc] init];
    topRight.frame = CGRectMake(topLeft.frame.origin.x+topLeft.frame.size.width+10.0f, 15.0f, cWidth, cWidth/7.0f*6.0f);
    //待确认关键工序:ToBeConfirmedCriticalActivity
    if ([self isHasUserPrivilege:@"ToBeConfirmedCriticalActivity"]) {
        topRight.backgroundColor = kRGB(90, 115, 196);
    }else{
        topRight.backgroundColor = kNoUserPrivilegeColor;
    }
    //topRight.backgroundColor = kRGB(90, 115, 196);
    topRight.layer.cornerRadius = 5.0f;
    topRight.layer.masksToBounds = YES;
    topRight.tag = 186;
    [topRight addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:topRight];
    
    UIImageView *topRightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02daiqueren"]];
    topRightImageView.frame = CGRectMake(10.0f, (topRight.frame.size.height-60)/2.0f, 116/3.0f, 132/3.0f);
    [topRight addSubview:topRightImageView];
    
    UILabel *topRightTitleLbl = [[UILabel alloc] init];
    topRightTitleLbl.frame = CGRectMake(topRightImageView.frame.origin.x+topRightImageView.frame.size.width+5.0f, topRightImageView.frame.origin.y, 69, 60.0f);
    topRightTitleLbl.text = @"关键工序确认";
    topRightTitleLbl.font = kAppFont(Home_Big_Size);
    topRightTitleLbl.textColor = kWhiteColor;
    topRightTitleLbl.numberOfLines = 0;
    [topRight addSubview:topRightTitleLbl];
    
    
    
    
    
    UIControl *bottomRight = [[UIControl alloc] init];
    bottomRight.frame = CGRectMake(topRight.frame.origin.x, topRight.frame.origin.y+topRight.frame.size.height+10.0f, cWidth, cWidth/7.0f*6.0f);
    //实体采集:ProjectCollect
    if ([self isHasUserPrivilege:@"ProjectCollect"]) {
        bottomRight.backgroundColor = kRGB(113, 125, 162);
    }else{
        bottomRight.backgroundColor = kNoUserPrivilegeColor;
    }
    //bottomRight.backgroundColor = kRGB(113, 125, 162);
    bottomRight.layer.cornerRadius = 5.0f;
    bottomRight.layer.masksToBounds = YES;
    bottomRight.tag = 189;
    [bottomRight addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:bottomRight];
    
    UIImageView *bottomRightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"04caiji"]];
    bottomRightImageView.frame = CGRectMake(10.0f, (bottomRight.frame.size.height-82/2.0f-40.0f)/2.0f, 86/2.0f, 82/2.0f);
    [bottomRight addSubview:bottomRightImageView];
    
    UILabel *bottomRightTitleLbl = [[UILabel alloc] init];
    bottomRightTitleLbl.frame = CGRectMake(bottomRightImageView.origin.x, bottomRightImageView.bottom+5.0f, 69.0f, 40.0f);
    bottomRightTitleLbl.text = @"实体情况采集";
    bottomRightTitleLbl.font = kAppFont(Home_Big_Size);
    bottomRightTitleLbl.textColor = kWhiteColor;
    bottomRightTitleLbl.numberOfLines = 0;
    [bottomRight addSubview:bottomRightTitleLbl];
 
    contentView.contentSize = CGSizeMake(kDeviceWidth, cWidth/5.0f*4.0f+cWidth/3.0f*2.0f+15.0f+30.0f);
}

#pragma mark ***************************** Action *********************************
- (void)senderAction:(UIControl*)sender
{
    switch (sender.tag) {
        case 166:
        {
            //关键工序验收:ToBeAcceptenceCriticalActivity
            if ([self isHasUserPrivilege:@"ToBeAcceptenceCriticalActivity"]) {
                CheckAndAcceptViewController *checkVC = [[CheckAndAcceptViewController alloc] init];
                checkVC.navTitle = @"关键工序查询";
                [self.navigationController pushViewController:checkVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 169:
        {
            //关键工序上传:UploadCriticalActivity
            if ([self isHasUserPrivilege:@"UploadCriticalActivity"]) {
                UploadGongXuViewController *uploadVC = [[UploadGongXuViewController alloc] init];
                uploadVC.navTitle = @"关键工序上传";
                [self.navigationController pushViewController:uploadVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 111:
        {
            //实体情况上传:UploadProjectConstrution
            if ([self isHasUserPrivilege:@"UploadProjectConstrution"]) {
                UploadShiTiQKCJViewController *jjjjVC = [[UploadShiTiQKCJViewController alloc] init];
                jjjjVC.navTitle = @"实体情况上传";
                [self.navigationController pushViewController:jjjjVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 186:
        {
            //待确认关键工序:ToBeConfirmedCriticalActivity
            if ([self isHasUserPrivilege:@"ToBeConfirmedCriticalActivity"]) {
                SureGongXuViewController *checkVC = [[SureGongXuViewController alloc] init];
                checkVC.navTitle = @"关键工序确认";
                [self.navigationController pushViewController:checkVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 189:
        {            
            //实体采集:ProjectCollect
            if ([self isHasUserPrivilege:@"ProjectCollect"]) {
                ShiTiQKCJViewController *shiVC = [[ShiTiQKCJViewController alloc] init];
                shiVC.navTitle = @"实体情况采集";
                [self.navigationController pushViewController:shiVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        default:
            break;
    }
}


- (void)rightBtnAcion
{
    
}

- (void)showAlertView{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您没有此模块的权限" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
    [alert show];
}

- (BOOL)isHasUserPrivilege:(NSString*)tempStr{
    NSArray *uuAry = [[UserInfo getInstance] getUserPrivilege];
    BOOL isExit = NO;
    for (int i = 0; i < uuAry.count; i ++) {
        NSString *str = [uuAry objectAtIndex:i];
        if ([str isEqualToString:tempStr]) {
            isExit = YES;
        }
    }
    
    if (isExit) {
        return YES;
    }else{
        return NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
